package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;

/**
 * @Author lvFF
 * @Date 2022/04/26/下午 4:22;
 * Description
 */
@RestController
@RequestMapping("category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加菜品分类和添加套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public  R<String> save(@RequestBody Category category){
        log.info("接受到没{}",category);
       /* category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(1L);
        category.setUpdateUser(1L);*/
        /*Long category1 = (Long) request.getSession().getAttribute("category");
        category.setUpdateUser(category1);
        category.setCreateUser(category1);*/
        log.info("获取到的 category--------------{}",category);
        categoryService.save(category);

        return R.success("添加分类成功");
    }

    /**
     *分页查询（带排序条件）
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page<Category>> page(Integer page,Integer pageSize){
        Page<Category> categoryPage = new Page<>(page, pageSize);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort").orderByDesc("update_time");
        Page<Category> page1 = categoryService.page(categoryPage, queryWrapper);

        return R.success(page1);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping
    public  R<String> delete(Long id){
        log.info("接受的id为{}",id);
       /* boolean b = categoryService.removeById(id);
        log.info("删除成功没{}" ,b);*/
        categoryService.remove(id);
        return R.success("删除成功");
    }@PutMapping
    public  R<String > update(@RequestBody Category category){
       /* category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(1L);
        category.setUpdateUser(1L);*/
        categoryService.updateById(category);
        return R.success("修改信息成果");
    }

    /**
     * 菜品选择单
     * @param type
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> addList(Integer type){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件
       // queryWrapper.like("type",type);
        queryWrapper.eq(type !=null,Category::getType,type);

        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
